package com.zc.msg.controller;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by eversec on 2018/3/19.
 */
@RestController
@RequestMapping("/kafka")
public class CollectController {

    protected static final Logger logger = LoggerFactory.getLogger(CollectController.class);

    @Autowired
    private KafkaTemplate<String, String> template;


    @RequestMapping(value = "/send", method = RequestMethod.GET)
    public void sendKafka(HttpServletRequest request, HttpServletResponse response) {
        try {
            String message = request.getParameter("message");
            logger.info("kafka的消息={}"+ message);
            template.send("test",message);
            logger.info("发送kafka成功");
        } catch (Exception e) {
            logger.error("发送kafka失败");
        }
    }
    @KafkaListener(topics = "test")
    public void listen(ConsumerRecord<?, ?> cr) throws Exception {
        logger.info(cr.toString());

    }
}
